<template>
  <div class="page-container">
    <pf-table
      ref="pfTable"
      v-model="form"
      row-key="id"
      default-expand-all
      :show-pagination="false"
      :columns="columns"
      :form-items="formItems"
      :tree-props="{children: 'categoryTreeVOS'}"
      @get-table-data="getTableData"
    >
      <template #handle-area>
        <el-button type="primary" @click="addHandle">新增类目</el-button>
      </template>
      <template #handle="{row}">
        <el-button type="text" size="small" @click="editHandle(row)">编辑类目</el-button>
      </template>
    </pf-table>
    <add-Category
      v-if="showCategory"
      :id="categoryId"
      @close="closeHandle"
      @confirm="confirmHandle"
    />
  </div>
</template>

<script>
import { getCategoryList } from '@/api/dictionary'
import AddCategory from './AddCategory.vue'

export default {
  name: 'CategoryList',
  components: {
    AddCategory
  },
  data() {
    return {
      form: {},
      categoryId: '',
      showCategory: false,
      loading: false
    }
  },
  computed: {
    columns() {
      const attrs = { 'min-width': '150px' }
      return [
        { label: '类目名称', prop: 'name', attrs },
        { label: '创建时间', prop: 'createTime', attrs },
        { label: '操作', slot: 'handle', attrs: { width: '150px' }}
      ]
    },
    formItems() {
      return [
        {
          type: 'input',
          label: '类目名称',
          prop: 'name'
        }
      ]
    }
  },

  methods: {

    getTableData({ ...formQuery }, done) {
      this.loading = true
      const data = {
        ...formQuery
      }
      getCategoryList(data).then(res => {
        this.loading = false
        done({ data: res || [] })
      }).catch(() => {
        done({ data: [] })
        this.loading = false
      })
    },

    // 新增类目
    addHandle() {
      this.categoryId = ''
      this.showCategory = true
    },

    // 编辑
    editHandle(row) {
      this.categoryId = row.id
      this.showCategory = true
    },

    // 关闭弹窗
    closeHandle() {
      this.showCategory = false
    },

    // 确定
    confirmHandle() {
      this.$refs.pfTable.getTableData()
    }
  }
}
</script>

<style lang="scss" scoped></style>
